package room

import (
	"game_server/entity"
	"game_server/framework/global"
	"game_server/tool"
	"github.com/spf13/viper"
)

func QueryAll() []entity.GameRoomEntity {
	var result []entity.GameRoomEntity
	gameType := viper.GetInt("environment.game-type")
	global.MysqlClient.Where("status = ? and game_type = ?", 1, gameType).Find(&result)
	return result
}

func QueryByParams(roomEntity *entity.GameRoomEntity, pageIndex int, pageSize int) []entity.GameRoomEntity {
	if pageIndex < 1 {
		pageIndex = 1
	}
	if pageSize < 1 {
		pageSize = 10
	}
	var result []entity.GameRoomEntity
	global.MysqlClient.Model(roomEntity).Where(roomEntity).Order("updated_at desc").Offset(pageIndex - 1).Limit(pageSize).Find(&result)
	return result
}

func UpdateById(roomEntity *entity.GameRoomEntity) {
	if roomEntity.Id == 0 {
		return
	}
	roomEntity.UpdatedAt = tool.CurrentDateTime()
	global.MysqlClient.Model(&entity.GameRoomEntity{}).Where("id=?", roomEntity.Id).Updates(roomEntity)
}
